package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.SourceTypeEnum;
import com.sky.domain.UmsMember;
import com.sky.domain.UmsMemberLevel;
import com.sky.dto.member.UmsMemberVO;
import com.sky.entity.GiteeEntity;
import com.sky.entity.GiteeUserInfoEntity;
import com.sky.exception.UsernameExistException;
import com.sky.service.UmsMemberLevelService;
import com.sky.service.UmsMemberService;
import com.sky.mapper.UmsMemberMapper;
import com.sky.utils.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【ums_member(会员)】的数据库操作Service实现
* @createDate 2024-06-30 16:45:18
*/
@Service
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember> implements UmsMemberService{

    @Resource
    private UmsMemberLevelService umsMemberLevelService;

    @Override
    public PageUtils queryPage(UmsMemberVO vo) {
        return null;
    }

    /**
     * 添加会员
     *
     * @param vo
     * @return
     */
    @Override
    public boolean addUmsMember(UmsMemberVO vo) {
        UmsMember member = baseMapper.selectOne(new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getUsername, vo.getUsername()));
        if (member != null) {
            // 存在用户名相同的用户
            throw new UsernameExistException();
        }
        UmsMember umsMember = new UmsMember();
        BeanUtil.copyProperties(vo, umsMember);

        // 查询会员的默认等级信息
        UmsMemberLevel defaultLevel = umsMemberLevelService.getDefaultLevel();
        umsMember.setLevelId(defaultLevel.getId());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        umsMember.setPassword(passwordEncoder.encode(umsMember.getPassword()));
        umsMember.setStatus(1);

        baseMapper.insert(umsMember);
        return true;
    }

    /**
     * 用户登录
     *
     * @param vo
     * @return
     */
    @Override
    public UmsMember login(UmsMemberVO vo) {
        UmsMember member = baseMapper.selectOne(new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getUsername, vo.getUsername()));
        if (member != null) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            if (passwordEncoder.matches(vo.getPassword(), member.getPassword())) {
                return member;
            }
        }
        return null;
    }

    /**
     * gitee用户登录
     *
     * @param giteeEntity
     * @return
     */
    @Override
    public UmsMember giteeLoginByToken(GiteeEntity giteeEntity) {
        GiteeUserInfoEntity userInfoEntity = giteeEntity.getUserInfoEntity();
        if (userInfoEntity != null) {
            // 查询使用gitee第三方登录的用户是否已经注册，如果没有注册，则先进行用户信息注册
            UmsMember umsMember = baseMapper.selectOne(new LambdaQueryWrapper<UmsMember>()
                    .eq(UmsMember::getSocialUid, userInfoEntity.getId())
                    .eq(UmsMember::getSourceType, SourceTypeEnum.GITEE.getSourceType()));
            if (umsMember == null) {
                // 注册新的用户信息
                umsMember = new UmsMember();
                umsMember.setSocialUid(userInfoEntity.getId().toString());
                umsMember.setAccessToken(giteeEntity.getAccess_token());
                umsMember.setExpiresIn(giteeEntity.getExpires_in());
                // 设置新用户名称
                UmsMember member = baseMapper.selectOne(new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getUsername, userInfoEntity.getLogin()));
                if (member != null){
                    member = baseMapper.selectOne(new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getUsername, userInfoEntity.getLogin() + "_" + userInfoEntity.getId()));
                    if (member != null){
                        umsMember.setUsername(userInfoEntity.getLogin() + "_" + userInfoEntity.getId() + "_" + IdUtil.getSnowflake().nextId());
                    } else {
                        umsMember.setUsername(userInfoEntity.getLogin() + "_" + userInfoEntity.getId());
                    }
                } else {
                    umsMember.setUsername(userInfoEntity.getLogin());
                }
                umsMember.setNickname(userInfoEntity.getName());
                umsMember.setSourceType(SourceTypeEnum.GITEE.getSourceType());
                umsMember.setHeader(userInfoEntity.getAvatar_url());

                // 查询会员的默认等级信息
                UmsMemberLevel defaultLevel = umsMemberLevelService.getDefaultLevel();
                umsMember.setLevelId(defaultLevel.getId());
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                umsMember.setPassword(passwordEncoder.encode("123456"));
                umsMember.setStatus(1);

                baseMapper.insert(umsMember);
            } else {
                umsMember.setAccessToken(giteeEntity.getAccess_token());
                umsMember.setExpiresIn(giteeEntity.getExpires_in());
                baseMapper.updateById(umsMember);
            }
            return umsMember;
        }
        return null;
    }
}




